package com.example.demo1;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edit_username;
    private EditText edit_password;
    private Button button_login;
    private Button button_register;
    String name,psd,error;
    DBHelper helper;
    SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        helper=new DBHelper(this);
    }

    private void initView() {
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_password = (EditText) findViewById(R.id.edit_password);
        button_login = (Button) findViewById(R.id.button_login);
        button_register = (Button) findViewById(R.id.button_register);

        button_login.setOnClickListener(this);
        button_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        psd=edit_password.getText().toString();
        name=edit_username.getText().toString();
        Intent intent=null;
        switch (v.getId()){
            case R.id.button_login:
                //Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG).show();
                if(name.isEmpty()||name==null){
                    error="姓名不可为空";
                    return;
                }
                else if(psd.isEmpty()||psd==null){
                    error="密码不可为空";
                    return;
                }
                User user =new User();
                user.setName(name);
                user.selectByNameBySQLite(this);

                if(psd.equals(user.getPwd())) {
                    Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                    intent=new Intent(MainActivity.this,UserActivity.class);
                    intent.putExtra("username",user.getName());
                    intent.putExtra("phone",user.getPhone());
                    intent.putExtra("pwd",user.getPwd());
                    intent.putExtra("email",user.getEmail());
                    startActivity(intent);
                    finish();
                }
                else {
                    Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                break;

            case R.id.button_register:

                /*
                *
                Intent intent =new Intent(reg.this,ShowActivity.class);
                intent.putExtra("bundle",bundle);
                startActivity(intent);
                * */
                intent=new Intent(MainActivity.this,reg.class);
                startActivityForResult(intent,101);
                break;
        }


    }




    //处理返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==101&&resultCode==102){
            Bundle bundle=data.getBundleExtra("bundle");
            edit_username.setText( bundle.getString("username"));
            edit_password.setText( bundle.getString("pwd"));
        }
        Log.i("tag","处理返回数据");

    }

    //状态测试
    /*
    onRestart
    onPause
    onStop
    onResume
    onDestroy
    onStart

    // */
    @Override
    protected void onResume() {
        Log.i("tag","onResume");

        User user=new User();
        //SharedPreferences方式自动填写
        user.getUserBySharedPreferences(this);

        //文件存储自动填写
        //user.getUserByFile(this);
        edit_username.setText( user.getName());
        edit_password.setText( user.getPwd());
        super.onResume();
    }



}
